#!/usr/bin/env python

import os
import sys
import unittest
from pyrna.design import CfgRNADesign
from pyrna.param import Param
from pyrna import fsalib
from pyrna import utils
from data import data_dir

utils.disable_loglevel('DEBUG')
utils.debug_off()

fbdnsample = os.path.join(data_dir, 'fbdnsample.motifs')
fbdnsample1 = os.path.join(data_dir, 'fbdnsample1.motifs')

class CfgRNADesign_Test(unittest.TestCase):
    """
    Run tests on different cases.
    """
    def setUp(self):
        self.outdir = os.path.join(data_dir, 'results')
        self.params = (
        {'seq'      : 'NNNNNNN',
         'struct'   : '.((.)).',
         'mda_motif': '',
         'fbdn'     : '',
         'fsa_seq'  : True,
         'num_expected': 0,
         },
        {'seq'      : 'NNNNNNNNN',
         'struct'   : '.((..))..',
         'mda_motif': '',
         'fbdn'     : '',
         'fsa_seq'  : True,
         },
        {'seq'      : 'ANCCNNNNG',
         'struct'   : '.((..))..',
         'mda_motif': '',
         'fbdn'     : '',
         'fsa_seq'  : True,
         },
        {'seq'      : 'NNNNNNNNN',
         'struct'   : '.((..))..',
         'mda_motif': '',
         'fbdn'     : fbdnsample,
         'fsa_seq'  : True,
         },
        {'seq'      : 'NCCNNN',
         'struct'   : '(.)(.)',
         'mda_motif': 'CC',
         'fbdn'     : fbdnsample,
         'fsa_seq'  : True,
         },
        {'seq'      : 'ANCCNNNNG',
         'struct'   : '.((..))..',
         'mda_motif': 'CC',
         'fbdn'     : fbdnsample,
         'fsa_seq'  : True,
         },
        {'seq'      : 'ANNNCCNNNG',
         'struct'   : '.(((..))).',
         'mda_motif': 'CC',
         'fbdn'     : fbdnsample,
         'fsa_seq'  : True,
         },
        {'seq'      : 'ANCNNCNNG',
         'struct'   : '.((..))..',
         'mda_motif': '',
         'fbdn'     : '',
         'fsa_seq'  : True,
         },
        {'seq'      : 'ANCNNCNNG',
         'struct'   : '.((..))..',
         'mda_motif': '',
         'fbdn'     : fbdnsample,
         'fsa_seq'  : True,
         },
        {'seq'      : 'ANCNNGNNG',
         'struct'   : '.((..))..',
         'mda_motif': '',
         'fbdn'     : fbdnsample,
         'fsa_seq'  : True,
         },
        {'seq'      : 'ANCNNGNNG',
         'struct'   : '.((..))..',
         'mda_motif': '',
         'fbdn'     : fbdnsample1,
         'fsa_seq'  : True,
         },
        )
    
    def test_design(self):
        if not os.path.exists(self.outdir):
            os.mkdir(self.outdir)
            
        i = 0
        p = Param()
        for param in self.params:
            i += 1
            param['outprefix'] = os.path.join(self.outdir, str(i)) 
            p.set(param)
            p.check()
            
            print >> sys.stderr, 'Test %d: \n%s' % (i, p)

            designer = CfgRNADesign(p)
            designer.design()
            
            num_real = designer.get_num_candidates()

            if p.fsa_seq:
                f_seq = designer.get_fsa_seq()
                num_expect = 0
                for line in open(f_seq):
                    s = line.rstrip()
                    #print >> sys.stderr, s, p.struct
                    # filter and count only correct structures
                    if fsalib.validate_seq2struct(s, p.struct):
                        num_expect += 1
            
                print ('Number of sequences expected==real, %d==%d?: %s\n'
                       %(num_expect, num_real, (num_expect==num_real)))
                assert num_expect == num_real
                
            if p.fbdn:
                os.system('rm %s' % p.fbdn+'.*')
            os.system('rm -f %s' % p.outprefix+'*')
            
################################################################################

if __name__ == '__main__':
    unittest.main()
            